﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using SCMS.Model;
using SCMS.Resource;

namespace SCMS.UI.Models
{
    #region .RFA Model Classes.

    public class AdvanceRequestModel
    {
        public Guid Id { get; set; }

        public string RefNumber { get; set; }

        [DataType(DataType.Date)]
        public DateTime RequestDate { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid PaymentTermId { get; set; }

        public SelectList PaymentTerms { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid PaymentTypeId { get; set; }

        public SelectList PaymentTypes { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid PaidTo { get; set; }

        public SelectList StaffList { get; set; }

        public decimal TotalAdvance { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid CurrencyId { get; set; }

        public SelectList Currencies { get; set; }

        [Required]
        public string Subject { get; set; }

        public string Remarks { get; set; }

        [Required]
        public string Description { get; set; }

        public string DocDescription { get; set; }

        public Guid MBCurrency { get; set; }

        public decimal MBValue { get; set; }

        public string VoucherNo { get; set; }

        public string AccountNo { get; set; }

        public Guid PreparedBy { get; set; }

        public DateTime PreparedOn { get; set; }

        public List<AdvanceRequestBLModel> PaymentDetails { get; set; }
        public List<AdvanceRequestSupportingDocModel> SupportingDocuments { get; set; }
        public List<AdvanceRequestOtherDocModel> OtherDocuments { get; set; }

        //Display Properties
        public string Currency { get; set; }
        public string MBCurrencyShortName { get; set; }
        public string PaymentTerm { get; set; }
        public string PaymentType { get; set; }
        public string Payee { get; set; }

        //Staff Details
        public Staff Preparer { get; set; }
        public Staff Approver { get; set; }
        public DateTime ApprovedOn { get; set; }
        public Staff Authorizer { get; set; }
        public DateTime AuthorizedOn { get; set; }
        public DateTime PostedOn { get; set; }

        public bool IsAuthorized { get; set; }
        public bool FundsPosted { get; set; }
        public bool IsReviewed { get; set; }
        public bool IsSubmitted { get; set; }
        public bool IsRejected { get; set; }

        public bool CanEdit { get; set; }

        public bool CanApprove { get; set; }

        public bool CanPostFunds { get; set; }

        public List<Model.BudgetCheckResult> BudgetCheckResults { get; set; }
    }

    public class AdvanceRequestBLModel
    {
        public Guid Id { get; set; }

        public Guid AdvanceRequestId { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid BudgetLineID { get; set; }
        public string BudgetLine { get; set; }

        public SelectList ProjectNos { get; set; }
        public string ProjectNo { get; set; }
        public Guid ProjectId { get; set; }
        
        public SelectList BudgetLines { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid AdvanceOptionId { get; set; }
        public string AdvanceOption { get; set; }

        public SelectList AdvanceOptions { get; set; }

        public string Description { get; set; }

        [Required]
        [DataType(DataType.Currency)]
        public decimal Amount { get; set; }
    }

    public class AdvanceRequestSupportingDocModel
    {
        public Guid Id { get; set; }

        public Guid AdvanceRequestId { get; set; }

        [Required]
        public Guid DocumentId { get; set; }
        public string DocumentType { get; set; }

        public SelectList DocumentTypes { get; set; }

        public string RefNumber { get; set; }

        public Guid RefDocumentId { get; set; }

        public SelectList RefNumbers { get; set; }
    }

    public class AdvanceRequestOtherDocModel
    {
        public Guid Id { get; set; }

        [Required]
        public string DocumentType { get; set; }

        public string RefNumber { get; set; }

        public string Description { get; set; }

        public Guid AdvanceRequestId { get; set; }
    }

    #endregion

    #region .Extensions (Helpers) 

    public static class AdvanceRequestExtension
    { 
        public static AdvanceRequestModel ToModel(this AdvanceRequest  rfa)
        {
            return new AdvanceRequestModel
            {
                Id = rfa.Id,
                RefNumber = rfa.RefNumber,
                RequestDate = rfa.RequestDate,
                PaymentTermId = rfa.PaymentTermId,
                PaymentTerm = rfa.PaymentTerm.Description,
                PaymentTypeId = rfa.PaymentTypeId,
                PaymentType = rfa.PaymentType.Description,
                PaidTo = rfa.PaidTo,
                Payee = rfa.Staff.Person.FirstName + " " + rfa.Staff.Person.OtherNames,
                TotalAdvance = rfa.TotalAdvance,
                CurrencyId = rfa.CurrencyId,
                Currency = rfa.Currency.ShortName,
                MBCurrency = rfa.MBCurrency.HasValue ? rfa.MBCurrency.Value : Guid.Empty,
                Subject = rfa.Subject,
                Remarks = rfa.Remarks,
                Description = rfa.Description,
                DocDescription = rfa.DocDescription,
                PreparedBy = rfa.PreparedBy,
                PreparedOn = (DateTime)rfa.PreparedOn,
                Preparer = rfa.Staff2,
                Approver = rfa.Staff3,
                Authorizer = rfa.Staff4,
                ApprovedOn = rfa.ReviewedOn != null ? (DateTime)rfa.ReviewedOn : DateTime.Now,
                AuthorizedOn = rfa.AuthorizedOn != null ? (DateTime)rfa.AuthorizedOn : DateTime.Now,
                IsAuthorized = rfa.IsAuthorized != null ? (bool)rfa.IsAuthorized : false,
                FundsPosted = rfa.FundsPosted,
                IsRejected = (bool)rfa.IsRejected,
                IsReviewed = (bool)rfa.IsReviewed,
                IsSubmitted = rfa.IsSubmitted,
                PostedOn = rfa.PostedOn != null ? rfa.PostedOn.Value : DateTime.Now,
                MBValue = rfa.MBTotal.HasValue ? rfa.MBTotal.Value : 0,
                MBCurrencyShortName = rfa.Currency1 != null ? rfa.Currency1.ShortName : "",
                VoucherNo = rfa.VoucherNo,
                AccountNo = rfa.AccountNo
            };
        }

        public static AdvanceRequest ToEntity(this AdvanceRequestModel rfaModel)
        {
            return new AdvanceRequest
            {
                Id = rfaModel.Id,
                RefNumber = rfaModel.RefNumber,
                RequestDate = rfaModel.RequestDate,
                PaymentTermId = rfaModel.PaymentTermId,
                PaymentTypeId = rfaModel.PaymentTypeId,
                PaidTo = rfaModel.PaidTo,
                TotalAdvance = rfaModel.TotalAdvance,
                CurrencyId = rfaModel.CurrencyId,
                Subject = rfaModel.Subject,
                Remarks = rfaModel.Remarks,
                Description = rfaModel.Description,
                DocDescription = rfaModel.DocDescription,
                PreparedBy = rfaModel.PreparedBy,
                PreparedOn = rfaModel.PreparedOn
            };
        }
    }

    public static class AdvanceRequestBLExtension
    {
        public static AdvanceRequestBLModel ToModel(this AdvanceRequestBudgetLine rfaBL)
        {
            return new AdvanceRequestBLModel
            {
                Id = rfaBL.Id,
                AdvanceRequestId = rfaBL.AdvanceRequestId,
                BudgetLineID = rfaBL.BudgetLineId,
                ProjectId = rfaBL.ProjectBudget.BudgetCategory.ProjectDonor.Id,
                BudgetLine = rfaBL.ProjectBudget.LineNumber + " " + rfaBL.ProjectBudget.Description,
                ProjectNo = rfaBL.ProjectBudget.BudgetCategory.ProjectDonor.ProjectNumber,
                AdvanceOptionId = rfaBL.AdvanceOptionId,
                AdvanceOption = rfaBL.AdvanceRequestOption.Name,
                Description = rfaBL.Description,
                Amount = rfaBL.Amount
            };
        }

        public static AdvanceRequestBudgetLine ToEntity(this AdvanceRequestBLModel rfaBLModel)
        {
            return new AdvanceRequestBudgetLine
            {
                Id = rfaBLModel.Id,
                AdvanceRequestId = rfaBLModel.AdvanceRequestId,
                BudgetLineId = rfaBLModel.BudgetLineID,
                AdvanceOptionId = rfaBLModel.AdvanceOptionId,
                Description = rfaBLModel.Description,
                Amount = rfaBLModel.Amount
            };
        }
    }

    public static class AdvanceRequestSupportDocExtension
    {
        public static AdvanceRequestSupportingDocModel ToModel(this AdvanceRequestSupportingDocument rfaSD)
        {
            return new AdvanceRequestSupportingDocModel
            {
                Id = rfaSD.Id,
                AdvanceRequestId = rfaSD.AdvanceRequestId,
                DocumentId = rfaSD.DocumentId,
                RefNumber = rfaSD.RefNumber,
                RefDocumentId = (Guid)rfaSD.RefDocumentId
            };
        }

        public static AdvanceRequestSupportingDocument ToEntity(this AdvanceRequestSupportingDocModel rfaSDModel)
        {
            return new AdvanceRequestSupportingDocument
            {
                Id = rfaSDModel.Id,
                AdvanceRequestId = rfaSDModel.AdvanceRequestId,
                DocumentId = rfaSDModel.DocumentId,
                RefNumber = rfaSDModel.RefNumber,
                RefDocumentId = (Guid)rfaSDModel.RefDocumentId
            };
        }
    }

    public static class AdvanceRequestOtherDocExtension
    {
        public static AdvanceRequestOtherDocModel ToModel(this AdvanceRequestOtherDocument rfaOD)
        {
            return new AdvanceRequestOtherDocModel
            {
                Id = rfaOD.Id,
                AdvanceRequestId = rfaOD.AdvanceRequestId,
                DocumentType = rfaOD.DocumentType,
                RefNumber = rfaOD.RefNumber,
                Description = rfaOD.Description
            };
        }

        public static AdvanceRequestOtherDocument ToModel(this AdvanceRequestOtherDocModel rfaODModel)
        {
            return new AdvanceRequestOtherDocument
            {
                Id = rfaODModel.Id,
                AdvanceRequestId = rfaODModel.AdvanceRequestId,
                DocumentType = rfaODModel.DocumentType,
                RefNumber = rfaODModel.RefNumber,
                Description = rfaODModel.Description
            };
        }
    }

    #endregion
}